package com.dys.controller;

import com.dys.entity.PageBean;
import com.dys.entity.Student;
import com.dys.service.StudentService;
import com.dys.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @className: com.dys.controller.StudentController
 * @description: com.dys.controller
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/09/25 20:19
 */
@WebServlet(urlPatterns = "/studentServlet.do")
public class StudentController extends HttpServlet {
    private final StudentService studentService = new StudentServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 接收数据：1.页码数 2.页大小
        int currentPage = 1;
        int pageSize = 5;
        try {
            currentPage = Integer.parseInt(req.getParameter("currentPage"));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }

        try {
            pageSize = Integer.parseInt(req.getParameter("pageSize"));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        // 查询条件
        String stuname = req.getParameter("stuname");
        String stuage = req.getParameter("stuage");
        // 调用service层处理业务逻辑
        PageBean<Student> pageInfo = studentService.findByPage(stuname, stuage, currentPage, pageSize);

        // 将数据放到请求域中
        req.setAttribute("pageBean", pageInfo);
        // 将查询条件放到请求域中
        req.setAttribute("stuname", stuname);
        req.setAttribute("stuage", stuage);
        // 响应数据,页面跳转
        req.getRequestDispatcher("showStudent.jsp").forward(req, resp);
    }
}
